package com.dayou.service.impl;

import java.util.List;
import java.util.regex.Pattern;

import com.dayou.bean.SysDept;
import com.dayou.bean.SysRole;
import com.dayou.bean.SysUser;
import com.dayou.common.exception.BusinessException;
import com.dayou.common.utils.SHAUtil;
import com.dayou.common.utils.ServletUtils;
import com.dayou.common.utils.StringUtils;
import com.dayou.mapper.SysDeptMapper;
import com.dayou.mapper.SysRoleMapper;
import com.dayou.mapper.SysUserMapper;
import com.dayou.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dayou.service.ISysUserService;
import com.dayou.common.core.text.Convert;

/**
 * 达幽系统集成框架 - 用户 服务层实现
 * 
 * @author dayou
 * @date 2020-04-23
 */
@Service
public class SysUserService implements ISysUserService
{
	@Autowired
	private SysUserMapper sysUserMapper;
	@Autowired
	private SysDeptMapper sysDeptMapper;
	@Autowired
	private SysRoleMapper sysRoleMapper;

	/**
     * 查询达幽系统集成框架 - 用户信息
     * 
     * @param id 达幽系统集成框架 - 用户ID
     * @return 达幽系统集成框架 - 用户信息
     */
    @Override
	public SysUser selectSysUserById(Integer id)
	{
	    return sysUserMapper.selectSysUserById(id);
	}
	
	/**
     * 查询达幽系统集成框架 - 用户列表
     * 
     * @param sysUser 达幽系统集成框架 - 用户信息
     * @return 达幽系统集成框架 - 用户集合
     */
	@Override
	public List<SysUser> selectSysUserList(SysUser sysUser)
	{
		List<SysUser> users = sysUserMapper.selectSysUserList(sysUser);
		users.forEach(u->u.setPassword(null));
		return users;
	}

	@Override
	public SysUser login(SysUser sysUser) {
		String account = sysUser.getAccount();
		if(StringUtils.isEmpty(account)){
			throw new BusinessException("用户账号不可为空");
		}
		String password = sysUser.getPassword();
		if(StringUtils.isEmpty(password)){
			throw new BusinessException("用户密码不可为空");
		}else{
			sysUser.setPassword(null);
		}
		List<SysUser> users = sysUserMapper.selectSysUserList(sysUser);
		if(users.size() > 1){
			throw new BusinessException("检测到重复的账号，已停用该账号");
		} else if(users.size() == 1){
			SysUser user = users.get(0);
			int offset = user.getOffset();
			String passwordEnc = SHAUtil.SHAEncrypt(password,offset);
			if(user.getPassword().equals(passwordEnc)){
				//在会话中加入登录用户的信息，浏览器关闭后会自动清除
				ServletUtils.getRequest().getSession().setAttribute("login_user",user);
				return user;
			}else{
				throw new BusinessException("用户名或密码错误");
			}
		}else{
			throw new BusinessException("用户不存在");
		}
	}

	/**
     * 新增达幽系统集成框架 - 用户
     * 
     * @param sysUser 达幽系统集成框架 - 用户信息
     * @return 结果
     */
	@Override
	public int insertSysUser(SysUser sysUser)
	{
		String account = sysUser.getAccount();
		if(StringUtils.isEmpty(account)){
			throw new BusinessException("用户账号不可为空");
		}
		String password = sysUser.getPassword();
		if(StringUtils.isEmpty(password)){
			throw new BusinessException("用户密码不可为空");
		}else{
			String pattern = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$";
			if(!Pattern.matches(pattern,password)){
				throw new BusinessException("用户密码至少需要6位，且必须包含字母和数字，不可包含特殊字符");
			}
		}
		String nickname = sysUser.getNickname();
		if(StringUtils.isEmpty(nickname)){
			throw new BusinessException("用户名不可为空");
		}
		Integer deptId = sysUser.getDeptId();
		if(null == deptId){
			throw new BusinessException("部门不可为空");
		}else{
			SysDept dept = sysDeptMapper.selectSysDeptById(deptId);
			if(null == dept){
				throw new BusinessException("部门不存在");
			}else{
				String rolesStr = sysUser.getRoles();
				if(StringUtils.isNotEmpty(rolesStr)){
					String[] rIds = Convert.toStrArray(rolesStr);
					List<SysRole> roles = sysRoleMapper.selectSysRoleByIds(rIds);
					if(roles.size() != rIds.length){
						throw new BusinessException("角色类型异常");
					}else{
						return insert(sysUser);
					}
				}else{
					return insert(sysUser);
				}
			}
		}
	}

	private int insert(SysUser sysUser){
		sysUser.setCreateBy(UserUtil.getUser().getId());
		int offset = (int)(Math.random()*(sysUser.getPassword().length()/2))+1;
		sysUser.setOffset(offset);
		sysUser.setPassword(SHAUtil.SHAEncrypt(sysUser.getPassword(),offset));
		return sysUserMapper.insertSysUser(sysUser);
	}
	
	/**
     * 修改达幽系统集成框架 - 用户
     * 
     * @param sysUser 达幽系统集成框架 - 用户信息
     * @return 结果
     */
	@Override
	public int updateSysUser(SysUser sysUser)
	{
		sysUser.setUpdateBy(UserUtil.getUser().getId());
	    return sysUserMapper.updateSysUser(sysUser);
	}

	/**
     * 删除达幽系统集成框架 - 用户对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteSysUserByIds(String ids)
	{
		return sysUserMapper.deleteSysUserByIds(Convert.toStrArray(ids));
	}
	
}
